// coding: utf-8
// @Author : lryself
// @Date : 2021/3/24 11:04
// @Software: GoLand

package main

import (
	"archive/zip"
	"fmt"
	"golang.org/x/text/encoding/simplifiedchinese"
	"io"
	"os"
	"path/filepath"
)

func main() {
	err := Unzip("F:\\PyCharm知识手册V1.0.zip", "f:\\")

	if err!=nil{
		fmt.Println(err)
	}
}

func Unzip(zipFile string, destDir string) error {
	zipReader, err := zip.OpenReader(zipFile)
	if err != nil {
		return err
	}
	defer zipReader.Close()

	for _, f := range zipReader.File {
		fName,err := simplifiedchinese.GBK.NewDecoder().String(f.Name)
		if err != nil{
			return err
		}
		fPath := filepath.Join(destDir, fName)
		if f.FileInfo().IsDir() {
			os.MkdirAll(fPath, os.ModePerm)
		} else {
			if err = os.MkdirAll(filepath.Dir(fPath), os.ModePerm); err != nil {
				return err
			}

			inFile, err := f.Open()
			if err != nil {
				return err
			}
			defer inFile.Close()

			outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
			if err != nil {
				return err
			}
			defer outFile.Close()

			_, err = io.Copy(outFile, inFile)
			if err != nil {
				return err
			}
		}
	}
	return nil
}